// src/controller/user.ts
// POST /user/ HTTP/1.1
// Host: localhost:3000
// Content-Type: application/json; charset=UTF-8
//
// {"uid": "1", "name": "harry"}
import {Controller, Inject, Get} from '@midwayjs/core';
import {Context} from '@midwayjs/koa';
import {User} from "../interface";

@Controller('/user')
export class UserController {

  @Inject()
  ctx: Context;
// src/controller/user.ts
// GET /user/1
  @Get('/:id/info')
  async getUser(): Promise<User> {
    console.info('this.ctx:', this.ctx)
    // Query
    const query: any = this.ctx.query;
    console.info('query:', query);
    const queryName: any = this.ctx.query.name;
    console.info('queryName:', queryName);
    // bodyParam
    const body = this.ctx.request.body;
    console.info('body:', body);
    // headerParam
    const cacheSetting = this.ctx.get('cache-control');
    console.info('cacheSetting:', cacheSetting);
    // Router Params
    const params = this.ctx.params;
    console.info('params:', params);
    // cookie
    // set cookie
    this.ctx.cookies.set('foo', 'bar', {encrypt: true});
    // get cookie
    this.ctx.cookies.get('foo', {encrypt: true});
    // Session
    // 获取 Session 上的内容
    // const userId =  this.ctx.session.userId;
    // const posts = await  this.ctx.service.get.fetch(userId);
    // console.info(posts)
    // 修改 Session 的值
    this.ctx.session.visited = this.ctx.session.visited ? (this.ctx.session.visited + 1) : 1;
    // return mock
    const userZch: User = {id: 1, name: 'a', age: 15};
    console.info('userZch:', userZch);
    return userZch;
  }
}
